package com.servlet;

import com.dao.UserDao;
import com.entity.User;
import com.util.MybatisUtil;
import org.apache.ibatis.session.SqlSession;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * @author admin
 */
@WebServlet({"/UserServlet.filter","/UserSelectOneServlet.filter","/UserDeleteServlet.filter",
        "/UserAddServlet.filter","/UserUpdateServlet.filter","/UserSelectAllServlet.filter"})
public class UserServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        System.out.println("UserServlet");
        String method = request.getParameter("method");
        System.out.println(method);
        if(method.equals("userSelectList")) {
            this.selectUserList(request, response);
        }else if (method.equals("userSelectOne")){
            this.selectUserOne(request, response);
        }else if (method.equals("deleteUser")){
            this.deleteUser(request, response);
        }else if (method.equals("addUser")){
            this.addUser(request, response);
        }else if (method.equals("updateUser")){
            this.updateUser(request, response);
        }else {
            request.getRequestDispatcher("/WEB-INF/page/user.jsp").forward(request,response);
        }

    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }

    private void selectUserList(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        SqlSession session = MybatisUtil.getSession();
        UserDao userDao = session.getMapper(UserDao.class);
        List<User> userList = userDao.selectUserList();
        request.setAttribute("userList",userList);
        request.getRequestDispatcher("/WEB-INF/page/user.jsp").forward(request,response);
    }

    private void selectUserOne(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        SqlSession session = MybatisUtil.getSession();
        UserDao userDao = session.getMapper(UserDao.class);
        String u_id = request.getParameter("u_id");
        User updateUser = userDao.selectUserOne(Integer.parseInt(u_id));
        System.out.println(updateUser.toString());
        request.setAttribute("updateUser",updateUser);
        request.getRequestDispatcher("/WEB-INF/page/user.jsp").forward(request,response);
    }

    private void deleteUser(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        SqlSession session = MybatisUtil.getSession();
        UserDao userDao = session.getMapper(UserDao.class);
        String u_id = request.getParameter("u_id");
        boolean b = userDao.deleteUser(Integer.parseInt(u_id));
        session.commit();
        if (b){
            System.out.println("用户删除成功");
            response.sendRedirect(request.getContextPath()+"/UserSelectAllServlet.filter?method=userSelectList");
        }
    }

    private void addUser(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        User user = new User();
        String u_name = request.getParameter("u_name");
        String u_pwd = request.getParameter("u_pwd");
        String u_age = request.getParameter("u_age");
        String u_sex = request.getParameter("addu_sex");
        user.setU_name(u_name);
        user.setU_pwd(u_pwd);
        user.setU_age(Integer.parseInt(u_age));
        user.setU_sex(u_sex);
        System.out.println(user);
        SqlSession session = MybatisUtil.getSession();
        UserDao userDao = session.getMapper(UserDao.class);
        userDao.insertUser(user);
        session.commit();
        System.out.println("用户添加成功");
        response.sendRedirect(request.getContextPath() + "/UserSelectAllServlet.filter?method=userSelectList");
    }

    private void updateUser(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        User user = new User();
        String u_id = request.getParameter("u_id");
        String u_name = request.getParameter("u_name");
        String u_pwd = request.getParameter("u_pwd");
        String u_age = request.getParameter("u_age");
        String u_sex = request.getParameter("u_sex");
        user.setU_id(Integer.parseInt(u_id));
        user.setU_name(u_name);
        user.setU_pwd(u_pwd);
        user.setU_age(Integer.parseInt(u_age));
        user.setU_sex(u_sex);
        SqlSession session = MybatisUtil.getSession();
        UserDao userDao = session.getMapper(UserDao.class);
        userDao.updateUser(user);
        session.commit();
        response.sendRedirect(request.getContextPath()+"/UserSelectAllServlet.filter?method=userSelectList");
    }
}
